package com.dayatang.android.Activity;

import java.util.concurrent.ExecutionException;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.view.View;

import com.dayatang.android.Chart.AbstractChart;

public class ConstructionProjectProductionValueActivity extends AbstractChart {
	
	private String[] projectName = null;
	private String[] projectOutput = null;
	
	protected void InitData(){
		String url = null;
		if(!fromDate.equals("") && !toDate.equals("")){
			url = getServerAddress() + "/remote/project-output.action?fromDate="+fromDate+"&toDate="+toDate;
		}else{
			url = getServerAddress() + "/remote/project-output.action";
		}
		try {
			String result = CheckServerData(url);
			if(result != null){
				JSONArray array = new JSONArray(result);
				projectName = new String[array.length()];
				projectOutput = new String[array.length()];
				for (int i = 0, len = array.length(); i < len; i++) {
					JSONObject jsonObject = array.getJSONObject(i);
					projectName[i] = jsonObject.getString("projectName");
					projectOutput[i] = jsonObject.getString("projectOutput");
				}
				FillContentView(execute());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
	
	public View execute() {
	    XYMultipleSeriesRenderer renderer = BuildBarRenderer(Color.RED);
	    setChartSettings(renderer,getString(R.string.reports_tab_one)+getString(R.string.reports_statistics),getString(R.string.project_name),getString(R.string.reports_y_axis_output),0.5,8.5,0,getArrayMax(projectOutput)*1.1);
	    for (int i = 1, len = projectOutput.length; i <= len; i++) {
	    	renderer.addXTextLabel(i, projectName[i-1]);
	    }
		return ChartFactory.getBarChartView(this, BuildBarDataset(projectName,projectOutput), renderer, Type.DEFAULT);
	}
	
}
